package com.ruoyi.ring.service.impl;

import cn.hutool.core.convert.Convert;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.ring.domain.RingMyOpinionFeedback;
import com.ruoyi.ring.domain.RingMyOpinionImage;
import com.ruoyi.ring.domain.dto.RingMyOpinionFeedbackDTO;
import com.ruoyi.ring.mapper.RingMyOpinionFeedbackMapper;
import com.ruoyi.ring.mapper.RingMyOpinionImageMapper;
import com.ruoyi.ring.service.IRingMyOpinionFeedbackService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 我的意见反馈Service业务层处理
 * 
 * @author liuwp
 * @date 2025-10-15
 */
@Service
public class RingMyOpinionFeedbackServiceImpl implements IRingMyOpinionFeedbackService 
{
    @Resource
    private RingMyOpinionFeedbackMapper ringMyOpinionFeedbackMapper;
    @Resource
    private RingMyOpinionImageMapper ringMyOpinionImageMapper;

    /**
     * 查询我的意见反馈
     * 
     * @param id 我的意见反馈主键
     * @return 我的意见反馈
     */
    @Override
    public RingMyOpinionFeedback selectRingMyOpinionFeedbackById(Long id)
    {
        return ringMyOpinionFeedbackMapper.selectRingMyOpinionFeedbackById(id);
    }

    /**
     * 查询我的意见反馈列表
     * 
     * @param ringMyOpinionFeedback 我的意见反馈
     * @return 我的意见反馈
     */
    @Override
    public List<RingMyOpinionFeedback> selectRingMyOpinionFeedbackList(RingMyOpinionFeedback ringMyOpinionFeedback)
    {
        return ringMyOpinionFeedbackMapper.selectRingMyOpinionFeedbackList(ringMyOpinionFeedback);
    }

    /**
     * 根据业务主键查询一条记录：我的意见反馈
     *
     * @param ringMyOpinionFeedback 我的意见反馈
     * @return 我的意见反馈
     */
    @Override
    public RingMyOpinionFeedback selectOneRingMyOpinionFeedback(RingMyOpinionFeedback ringMyOpinionFeedback)
    {
        return ringMyOpinionFeedbackMapper.selectOneRingMyOpinionFeedback(ringMyOpinionFeedback);
    }

    /**
     * 新增我的意见反馈
     * 
     * @param ringMyOpinionFeedback 我的意见反馈
     * @return 结果
     */
    @Override
    public int insertRingMyOpinionFeedback(RingMyOpinionFeedback ringMyOpinionFeedback)
    {
        ringMyOpinionFeedback.setCreateTime(DateUtils.getNowDate());
        return ringMyOpinionFeedbackMapper.insertRingMyOpinionFeedback(ringMyOpinionFeedback);
    }

    /**
     * 修改我的意见反馈
     * 
     * @param ringMyOpinionFeedback 我的意见反馈
     * @return 结果
     */
    @Override
    public int updateRingMyOpinionFeedback(RingMyOpinionFeedback ringMyOpinionFeedback)
    {
        ringMyOpinionFeedback.setUpdateTime(DateUtils.getNowDate());
        return ringMyOpinionFeedbackMapper.updateRingMyOpinionFeedback(ringMyOpinionFeedback);
    }

    @Override
    public int updateRingMyOpinionFeedbackCondition(RingMyOpinionFeedback ringMyOpinionFeedback, RingMyOpinionFeedback ringMyOpinionFeedbackCondition)
    {
                ringMyOpinionFeedback.setUpdateTime(DateUtils.getNowDate());
        return ringMyOpinionFeedbackMapper.updateRingMyOpinionFeedbackCondition(ringMyOpinionFeedback, ringMyOpinionFeedbackCondition);
    }

    /**
     * 批量删除我的意见反馈
     * 
     * @param ids 需要删除的我的意见反馈主键
     * @return 结果
     */
    @Override
    public int deleteRingMyOpinionFeedbackByIds(Long[] ids)
    {
        return ringMyOpinionFeedbackMapper.deleteRingMyOpinionFeedbackByIds(ids);
    }

    /**
     * 删除我的意见反馈信息
     * 
     * @param id 我的意见反馈主键
     * @return 结果
     */
    @Override
    public int deleteRingMyOpinionFeedbackById(Long id)
    {
        return ringMyOpinionFeedbackMapper.deleteRingMyOpinionFeedbackById(id);
    }

    /**
     * 条件删除我的意见反馈
     *
     * @param ringMyOpinionFeedback 我的意见反馈
     * @return 结果
     */
    @Override
    public int deleteRingMyOpinionFeedbackByCondition(RingMyOpinionFeedback ringMyOpinionFeedback)
    {
        return ringMyOpinionFeedbackMapper.deleteRingMyOpinionFeedbackByCondition(ringMyOpinionFeedback);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addMyOpinionFeedback(RingMyOpinionFeedbackDTO dto)
    {
        int result = 0;
        try {
            Date date = DateUtils.getNowDate();
            long userId = SecurityUtils.getUserId();
            RingMyOpinionFeedback ringMyOpinionFeedback = Convert.convert(RingMyOpinionFeedback.class,dto);
            ringMyOpinionFeedback.setUserId(userId);
            ringMyOpinionFeedback.setCreateTime(date);
            result = ringMyOpinionFeedbackMapper.insertRingMyOpinionFeedback(ringMyOpinionFeedback);
            long id = ringMyOpinionFeedback.getId();

            List<MultipartFile> fileList = dto.getFileList();
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            for (MultipartFile file : fileList)
            {
                // 上传并返回新文件名称
                String fileUrl = FileUploadUtils.upload(filePath, file);
                RingMyOpinionImage ringMyOpinionImage = new RingMyOpinionImage();
                ringMyOpinionImage.setOpinionId(id);
                ringMyOpinionImage.setDeviceSn(dto.getDeviceSn());
                ringMyOpinionImage.setUserId(userId);
                ringMyOpinionImage.setCreateTime(date);
                ringMyOpinionImage.setImageUrl(fileUrl);
                result = ringMyOpinionImageMapper.insertRingMyOpinionImage(ringMyOpinionImage);
            }
            return result;
        }catch (Exception e){
            throw new RuntimeException("创建意见反馈失败，请联系管理员！");
        }
    }
}
